﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using FlashCards.WebGUI.FlashCardService;

namespace FlashCards.WebGUI
{
    public partial class FlashCardDetails : System.Web.UI.Page
    {
        protected int WordId
        {
            get
            {
                return Convert.ToInt32(Request.QueryString["Id"]);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {                
                if (WordId != 0)
                {
                    Word word = new FlashCardsServiceClient().GetWord(WordId);

                    txtWord.Text = word.OriginalWord;
                    txtPronunciation.Text = word.Pronunciation;
                    txtDefinition.Text = word.Definition;
                    txtSampleSentence.Text = word.SampleSentence;

                    txtCreatedBy.Text = word.RecordedBy.Name;
                    txtCreatedAt.Text = word.RecordedAt.ToString("yyyy.MM.dd HH:mm:ss");
                    txtModifiedBy.Text = word.ModifiedBy.Name;
                    txtModifiedAt.Text = word.ModifiedAt.ToString("yyyy.MM.dd HH:mm:ss");
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Word word = null;

            if (WordId != 0)
            {
                word = new FlashCardsServiceClient().GetWord(WordId);
                word.OriginalWord = txtWord.Text;
                word.Pronunciation = txtPronunciation.Text;
                word.Definition = txtDefinition.Text;
                word.SampleSentence = txtSampleSentence.Text;
            }

            long wordId = new FlashCardsServiceClient().StoreWord(word);
            Response.Redirect(string.Format("FlashCardDetails.aspx?Id={0}", wordId));
        }
    }
}
